/*
 * @Author: your name
 * @Date: 2020-08-16 09:54:47
 * @LastEditTime: 2020-08-16 11:13:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \demo\day02\demo02\main.go
 */
package main

import "fmt"

// 结构体嵌套
type Address struct {
	Province string
	city     string
}

func (a Address) Area() {
	fmt.Println("地区")
}

type User struct {
	Name string
	Age  int
	Address
}

type Person struct {
	name   string
	age    int
	dreams []string
}

func (p *Person) SetDreams(dreams []string) {
	//
	// p.dreams = make([]string, len(dreams))
	// copy(p.dreams, dreams)
	p.dreams = dreams
}

// 一定要根据参数生成否则就会出现奇怪的问题
func NewUser() *User {
	return &User{}
}

func main() {
	// ------------------------------------------------
	u := NewUser()
	u.city = "哈哈" // 如果嵌套是匿名的话，那么外层可以调用内层的数据或者属性方法
	u.Province = "安徽 "
	u.Name = "啥"
	u.Age = 12
	fmt.Println(u)
	u.Area()
	// ------------------------------------------------
	// 这里注意
	u1 := NewUser()
	u1.city = "哈" // 如果嵌套是匿名的话，那么外层可以调用内层的数据或者属性方法
	u1.Province = "北京 "
	u1.Name = "你"
	u1.Age = 16
	fmt.Println(u)
	// ------------------------------------------------
	// slice 切片公用底层数组，所以修改的时候回影响值
	p1 := Person{name: "想王子", age: 18}
	data := []string{"吃饭", "睡觉", "打豆豆"}
	p1.SetDreams(data)
	fmt.Println(p1)
	data[1] = "不睡觉"
	fmt.Println(p1)
	// ------------------------------------------------
	p2 := Person{name: "旺旺", age: 12}
	data1 := []string{"订单", "ddd", "fff"}
	p2.SetDreams(data1)
	fmt.Println(p2)
	data1[1] = "不睡觉"
	fmt.Println(p2)
}
